package com.dmw.service.impl;

import com.dmw.dao.DmOrderDao;
import com.dmw.entity.DmOrder;
import com.dmw.entity.DmOrderLinkUser;
import com.dmw.service.DmOrderService;
import com.dmw.util.MD5;
import com.dmw.vo.orderVO.OrderInfoOutVo;
import com.dmw.vo.placeOrderVO.AddOrderVO;
import com.dmw.vo.placeOrderVO.OrderOfOrderNoVO;
import com.dmw.vo.placeOrderVO.OrderPaymentStateVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 订单表(DmOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-03-19 00:31:56
 */
@Service
public class DmOrderServiceImpl implements DmOrderService {
    @Resource
    private DmOrderDao dmOrderDao;

    @Override
    public  List<OrderOfOrderNoVO> queryOrderByOrderNo(Map<String, Object> map) throws Exception {
        return dmOrderDao.queryOrderByOrderNo(map);
    }

    @Override
    public List<OrderInfoOutVo> queryOrderList(Map<String, Object> map) throws Exception {
        return dmOrderDao.queryOrderList(map);
    }

    @Override
    public String createOrder(AddOrderVO vo) throws Exception {
        //添加order
        DmOrder order=new DmOrder();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String str="";
        for(int i=0;i<6;i++){
            str.concat(String.valueOf(Math.random()*10));
        }
        String orderNo= sdf.format(new Date())+ MD5.getMd5(str,6);
        order.setOrderNo(orderNo);
        order.setUserId(Long.valueOf(vo.getUserId()));
        order.setSchedulerId((Long.valueOf(vo.getSchedulerId())));
        order.setItemId(Long.valueOf(vo.getItemId()));
        order.setItemName(dmOrderDao.queryItemName(Long.valueOf(vo.getItemId())));//要查 item表
        order.setWxTradeNo(dmOrderDao.queryUserWX(Long.valueOf(vo.getUserId())));//要查 user表
        order.setAliTradeNo("123321");//要做支付
        order.setOrderType(0);
        order.setPayType("2");
        String[] s = vo.getSeatPositions().split(",");
        String[] linkIdS=vo.getLinkIds().split(",");
        order.setTotalCount(s.length);//要做string spilt判断数组长度
        order.setSourceType(vo.getSourceType());
        order.setIsNeedInvoice(vo.getIsNeedInvoice());
        order.setInvoiceType(vo.getInvoiceType());
        order.setInvoiceHead(String.valueOf(vo.getInvoiceHead()));
        order.setInvoiceNo(vo.getInvoiceNo());
        order.setIsNeedInsurance(vo.getIsNeedInsurance());
        Double price=0.0;
        List<DmOrderLinkUser> linkUsers=new ArrayList<>();
        for(int i=0;i<s.length;i++){
            String[] s1 = s[i].split("_");
            Map<String, Object> map=new HashMap<>();
            map.put("x",s1[0]);
            map.put("y",s1[1]);
            map.put("scheduleId",vo.getSchedulerId());
            Integer level=dmOrderDao.queryLevel(map);//要查等级
            map.clear();
            map.put("areaLevel",level);
            map.put("scheduleId",vo.getSchedulerId());
            Double priceNum=dmOrderDao.queryPrice(map);
            DmOrderLinkUser orderLinkUser=new DmOrderLinkUser();
            //获取userId

            //根据userId查user
            orderLinkUser.setLinkusername(dmOrderDao.queryLinkUserNameById(Long.valueOf(linkIdS[i])));
            orderLinkUser.setLinkuserid(Long.valueOf(linkIdS[i]));
            orderLinkUser.setX(Integer.valueOf(s1[0]));
            orderLinkUser.setY(Integer.valueOf(s1[1]));
            orderLinkUser.setPrice(priceNum);
            orderLinkUser.setCreatedtime(new Date());
            linkUsers.add(orderLinkUser);
            price=price+priceNum;
        }

        order.setTotalAmount(price);//要计算总价
        order.setInsuranceAmount(vo.getIsNeedInsurance()==1?20.0:0.0);
        order.setCreatedTime(new Date());
        dmOrderDao.addOrder(order);
        for(int i=0;i<linkUsers.size();i++){
            linkUsers.get(i).setOrderid(dmOrderDao.queryOrderId(orderNo));
            dmOrderDao.addOrderLinkUser(linkUsers.get(i));
        }
        return orderNo;
    }

    @Override
    public OrderPaymentStateVO queryOrderState(String orderNo) throws Exception {
        return dmOrderDao.queryOrderState(orderNo);
    }
}
